package com.njit.mailm.core.service;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

public class MailReciverBaseService {
	public Session createSession(String protocal,int port,String ip) {
		// 准备连接服务器的会话信息
		Properties props = new Properties();
		if(protocal == "pop3"){
			props.setProperty("mail.store.protocol", protocal); // 协议
			props.setProperty("mail.pop3.port", String.valueOf(port)); // 端口
			props.setProperty("mail.pop3.host", ip); // pop3服务器

		}else if(protocal == "smtp"){
			props.setProperty("mail.store.protocol", protocal); // 协议
			props.setProperty("mail.smtp.port", String.valueOf(port)); // 端口
			props.setProperty("mail.smtp.host", ip); // pop3服务器
			props.setProperty("mail.smtp.auth", "true");
		}

		// 创建Session实例对象
		return Session.getInstance(props);

	}
	
	public void close(Folder inbox,Store store){
		try {
			if (inbox != null) {
				inbox.close(true);
			}
			if (store != null) {
				store.close();
			}
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
